﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Dynamic;

namespace WpfApplication1.Controls
{
    /// <summary>
    /// Interaction logic for ProfileStatusControl.xaml
    /// </summary>
    public partial class ProfileStatusControl : UserControl
    {
        RootData rootdata = new RootData();
        CFacebookServer fbServer = new CFacebookServer(MainWindow.AccessToken);

        public ProfileStatusControl()
        {
            InitializeComponent();
            LoadStatus();
        }

        public void LoadStatus()
        {
            string result = "";
            IDictionary<string, object> source = fbServer.GetWall(30);
            if (source != null)
            {
                List<object> list_data = new System.Collections.Generic.List<object>();
                list_data = (List<object>)source["data"];
                for (int i = 0; i < list_data.Count; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)list_data[i];
                    IDictionary<string, object> temp_from = (IDictionary<string, object>)temp["from"];
                    string temp_id = (string)temp_from["id"];
                    if ((temp.ContainsKey("message"))&&(temp_id == (string)rootdata.personalInfo["id"]))
                        result = (string) temp["message"];
                }
            }
            textbox_message.Text = result;
            textbox_message.ToolTip = result;
        }

        void PostMessage()
        {
            if ((textbox_message.Text == "") || (textbox_message.Text == textbox_message.ToolTip))
            {
                MessageBox.Show("Your message must not be empty");
                return;
            }
            dynamic paramenters = new ExpandoObject();
            paramenters.message = textbox_message.Text;
            string result = fbServer.Post(rootdata.personalInfo["id"] + "/feed", paramenters);
            if (result != null)
            {
                MessageBox.Show("Message has been posted successfully!");
                // Reload messages
                textbox_message.ToolTip = textbox_message.Text;
            }
            else
            {
                MessageBox.Show("Can not post your message!");
            }
        }

        private void button_submit_Click(object sender, RoutedEventArgs e)
        {
            PostMessage();
        }

        private void textbox_message_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                PostMessage();
            else if (e.Key == Key.Escape)
                button_submit.Focus();
        }
    }
}
